﻿using System.Composition;
using System.Text.Json.Nodes;
using ExchangeClient.Abstractions;
using ExchangeClient.Abstractions.Model;
using HKH.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Bcpg;
using SyncService.Abstractions.Events;
using SyncService.Core.Tasks;

namespace SyncService.Core
{
    [Export(typeof(IEventHub))]
    public class EventHub : EventHubBase
    {
        public override void ProcessYideaEvent(RequestData requestData, IMemoryCache cache)
        {
            var syncEvent = AssetSyncEvent.Parse($"{requestData.EventId}|{requestData.TimeStamp}", requestData.Body);
             switch (syncEvent.EventType)
            {
                case EventType.WorkflowProcess:

                    var returnSrc = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "sample")?.Value;
                        if (returnSrc != null)
                        {
                            ParallelTaskPool.Enqueue(new SampleTask(returnSrc, requestData, cache) { Id = requestData.EventId });
                        }
                    
                    break;
                case EventType.Created:
                    var createSrc = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "sap")?.Value;
                    var BrandManagement = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "Brand")?.Value;

                    if (createSrc != null&&(syncEvent.SourceType==11|| syncEvent.SourceType == 0))
                    {
                        ParallelTaskPool.Enqueue(new SapTask(createSrc, requestData, cache) { Id = requestData.EventId });
                    }
                    else if (BrandManagement != null&& syncEvent.Data?["FormName"]?.ToString()== "主数据-品牌管理")
                    {
                        ParallelTaskPool.Enqueue(new BrandTask(BrandManagement, requestData, cache) { Id = requestData.EventId });
                    }


                        break;
                case EventType.ReceiptEffect:
                    var tosapSrc = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "sap")?.Value;
                    var assetInstorageSrc = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "assetInstorage")?.Value;
                    var inventoryInstotage = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "inventoryToInstorageService")?.Value;
                    var zeroInstotage = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "zeroToInstorage")?.Value;
                    var zeroResult = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "zeroResult")?.Value;
                    var MutiTransfer = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "mutiTransfer")?.Value;
                    var sapTransfer = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "SapTransfer")?.Value;
                    var sapTransferReceive = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "SapTransferReceive")?.Value;
                    if (inventoryInstotage != null && syncEvent.SourceType == 10 && syncEvent.Data["SerialNo"].ToString().Substring(0, 3) == "AIN") {
                        ParallelTaskPool.Enqueue(new AssetInstorageTask(inventoryInstotage, requestData, cache) { Id = requestData.EventId });
                    }
                    else if (assetInstorageSrc != null && syncEvent.Data["SerialNo"].ToString().Substring(0, 3) == "REC")
                    {
                        ParallelTaskPool.Enqueue(new AssetInstorageTask(assetInstorageSrc, requestData, cache) { Id = requestData.EventId });
                    }
                    else if (zeroInstotage != null && syncEvent.Data["SerialNo"].ToString().Substring(0, 3) == "ZER")
                    {
                        ParallelTaskPool.Enqueue(new AssetInstorageTask(zeroInstotage, requestData, cache) { Id = requestData.EventId });
                    }
                    else if (MutiTransfer != null && syncEvent.Data["SerialNo"].ToString().Substring(0, 3) == "MTR")
                    {
                        ParallelTaskPool.Enqueue(new MutiTransferTask(MutiTransfer, requestData, cache) { Id = requestData.EventId });
                    }
                    else if (zeroResult != null && syncEvent.Data["SerialNo"].ToString().Substring(0, 3) == "AIS")
                    {
                        ParallelTaskPool.Enqueue(new ZeroResultTask(zeroResult, requestData, cache) { Id = requestData.EventId });
                    }
                    else if (sapTransfer != null && syncEvent.Data["SerialNo"].ToString().Substring(0, 3) == "TRA" || syncEvent.Data["SerialNo"].ToString().Substring(0, 3) == "NTR")
                    {
                        ParallelTaskPool.Enqueue(new SapEffectTask(sapTransfer, requestData, cache) { Id = requestData.EventId });
                    }
                    else 
                    {
                        ParallelTaskPool.Enqueue(new SapEffectTask(tosapSrc, requestData, cache) { Id = requestData.EventId });
                    }
                    
                    break;
                default:
                    break;
            }

            if (syncEvent.Data!["SerialNo"]!=null&&syncEvent.Data!["SerialNo"].ToString().Substring(0, 3) == "REB") {
                var freeUpBudgetSrc = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "freeUpBudget")?.Value;
                ParallelTaskPool.Enqueue(new EsbTask(freeUpBudgetSrc, requestData, cache) { Id = requestData.EventId });
            } 
        }

        #region 同步调用，上线如果没有用需要使用这一板
        //public override void ProcessExternalEvent(RequestData requestData, IMemoryCache cache)
        //{
        //    if (requestData.Signature == "Assets")
        //    {
        //        var newreturnSrc = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "Asset")?.Value;
        //        if (newreturnSrc != null)
        //        {
        //            ParallelTaskPool.Enqueue(new AssetTask(newreturnSrc, requestData, cache) { Id = requestData.EventId });
        //        }
        //    }
        //    else
        //    {
        //        var flag = requestData.EventId.Substring(0, 3);
        //        var newreturnSrc = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "esb")?.Value;
        //        var updateBillReturnSrc = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "updateBill")?.Value;
        //        if (flag == "upd" && updateBillReturnSrc != null)
        //        {
        //            ParallelTaskPool.Enqueue(new UpdateBillTask(updateBillReturnSrc, requestData, cache) { Id = requestData.EventId });
        //        }
        //        else if (newreturnSrc != null)
        //        {
        //            ParallelTaskPool.Enqueue(new EsbTask(newreturnSrc, requestData, cache) { Id = requestData.EventId });
        //        }
        //    }
        //}
        #endregion




        //public override void ProcessExternalEvent(RequestData requestData, IMemoryCache cache)
        //{
        //    var syncEvent = AssetSyncEvent.Parse($"{requestData.EventId}|{requestData.TimeStamp}", requestData.Body);
        //    var sapTransferReceive = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "SapTransferReceive")?.Value;
        //    if (requestData.Signature == "Assets")
        //    {
        //        var newreturnSrc = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "Asset")?.Value;
        //        if (newreturnSrc != null)
        //        {
        //            ParallelTaskPool.Enqueue(new AssetTask(newreturnSrc, requestData, cache) { Id = requestData.EventId });
        //        }
        //    }
        //    else if (sapTransferReceive != null && syncEvent.Data["SerialNo"].ToString().Substring(0, 3) == "TRA" || syncEvent.Data["SerialNo"].ToString().Substring(0, 3) == "NTR")
        //    {
        //        ParallelTaskPool.Enqueue(new SapEffectTask(sapTransferReceive, requestData, cache) { Id = requestData.EventId });
        //    }
        //    else
        //    {
        //        var flag = requestData.EventId.Substring(0, 3);
        //        var newreturnSrc = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "esb")?.Value;
        //        var updateBillReturnSrc = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "updateBill")?.Value;
        //        if (flag == "upd" && updateBillReturnSrc != null)
        //        {
        //            ParallelTaskPool.Enqueue(new UpdateBillTask(updateBillReturnSrc, requestData, cache) { Id = requestData.EventId });
        //        }
        //        else if (newreturnSrc != null)
        //        {
        //            ParallelTaskPool.Enqueue(new EsbTask(newreturnSrc, requestData, cache) { Id = requestData.EventId });
        //        }
        //    }
        //}
        #region 新开发sap异步需要用到的
        public override void ProcessExternalEvent(RequestData requestData, IMemoryCache cache)
        {

            if (requestData.Signature == "Assets")
            {
                
                var newreturnSrc = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "Asset")?.Value;

                if (newreturnSrc != null)
                {
                    ParallelTaskPool.Enqueue(new AssetTask(newreturnSrc, requestData, cache) { Id = requestData.EventId });
                }
               
            }
            else if (requestData.Signature == "Async")
            {
                
                var syncEvent = JsonNode.Parse(requestData.Body).AsObject();
                var targetObj = syncEvent?["result"]?[0]?.AsObject();
                var syncEventstring = "";
                syncEventstring = syncEvent!["SerialNo"]!.ToString();
                var sapTransferReceive = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "SapTransferReceive")?.Value;
                if (sapTransferReceive != null)
                {
                    ParallelTaskPool.Enqueue(new AssetTask(sapTransferReceive, requestData, cache) { Id = requestData.EventId });
                }

            }
            else
            {
                var flag = requestData.EventId.Substring(0, 3);
                var newreturnSrc = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "esb")?.Value;
                var updateBillReturnSrc = Services.FirstOrDefault(x => x.Metadata[YdConstants.ID].ToString() == "updateBill")?.Value;
                if (flag == "upd" && updateBillReturnSrc != null)
                {
                    ParallelTaskPool.Enqueue(new UpdateBillTask(updateBillReturnSrc, requestData, cache) { Id = requestData.EventId });
                }
                else if (newreturnSrc != null)
                {
                    ParallelTaskPool.Enqueue(new EsbTask(newreturnSrc, requestData, cache) { Id = requestData.EventId });
                }
            }
        }
        #endregion


    }
}
